<?php 

class AdminPhotogalleryPresenter extends AdminPresenter{
	
	private $model;
	/**
	 * Start lifecycle presenter
	 *
	 * @return void
	 */
	protected function startup(){
		parent::startup();
		
		$this->template->jsFile[] = "inc/photogallery.js";

		$this->model = PhotogalleryModel::getInstance();
	}

	/**
	 * Settings for all render
	 *
	 * @return void
	 */
	protected function beforeRender(){

	}

	/**
	 * Render default action
	 *
	 * @return void
	 */
	public function renderDefault($id){
		
		// zjistime si id menu
		$menuItem = $this->system->getMenuItemByName($id);

		$info = $this->model->getInfo($menuItem[0]["id_menu"]);
		
		$this->template->id = $id;
		$this->template->add("info", $info);
	}
	
	
	public function actionSaveGallery(){
		
		$this->model->saveGallery($this->getParam("id"), $_POST["title"]);
		
		$this->flashMessage("Galerie byla přidána.", "ok");
		$this->redirect("AdminPhotogallery:default", $this->getParam("id"));
	}
	
	public function actionSaveGalleryDetail(){
		
		$pictures = array();
		
		$i = 0;
		foreach($_POST["id_files"] as $f){
			
			$pictures[] = array(
							"id_photogallery" => $_POST["id_photogallery"],
							"date" => new DateTime(),
							"title" => $_POST["files_title"][$i],
							"id_file" => $f,
							"hlavni" => ($_POST["hlavni"] == $f ? 1 : 0)
								);
			$i++;
		}
		
		$info = array(
					"title" => $_POST["title"],
					"text" => $_POST["text"]
					);
		
		// ulozime informace o galerii
		$this->model->updateGallery($_POST["id_photogallery"], $info);
		// nejdrive smazeme a pote znovu ulozime obrazky
		$this->model->deleteGalleryPictures($_POST["id_photogallery"]);
		if(!empty($pictures)) $this->model->saveGalleryPictures($pictures);
					
		$this->flashMessage("Galerie upravena.", "ok");
		$this->redirect("AdminPhotogallery:default", $this->getParam("id"));
	}
	
	public function actionDeletePhotogallery(){
		
		$id = $this->getParam("delete");
		
		$this->model->deletePhotogallery($id);
		$this->flashMessage("Galerie byla smazána.", "ok");
		$this->redirect("AdminPhotogallery:default", $this->getParam("id"));
	}
	
	public function renderPhotogallery(){
		
		$id = $this->getParam("edit");
		
		$edit = $this->model->getPhotogalleryInfo($id);
		$edit["pictures"] = $this->model->getPictures($id);
		
		$this->template->id = $this->getParam("id");
		$this->template->edit = $edit;
	}
	
}

?>